package uit.nfcmenu.db.adapter;

import java.util.ArrayList;

import uit.nfcmenu.db.model.MenuModel;
import uit.nfcmenu.util.ConvertImg;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;


public class MenuAdapter extends DatabaseAdapter {
	String tag = MenuAdapter.class.getSimpleName();

	public MenuAdapter(Context context) {
		super(context);
	}
	
	private MenuModel createMenuModel(Cursor cursor) {
		MenuModel model = new MenuModel();
		model.setId(cursor.getInt(cursor.getColumnIndex(DbDefines.TableMenu.menuId)));
		model.setName(cursor.getString(cursor.getColumnIndex(DbDefines.TableMenu.menuName)));
		model.setImageLink(cursor.getString(cursor.getColumnIndex(DbDefines.TableMenu.imageLink)));
		model.setDescription(cursor.getString(cursor.getColumnIndex(DbDefines.TableMenu.description)));
		Bitmap bitmap = ConvertImg.getBitmap(cursor.getBlob(cursor.getColumnIndex(DbDefines.TableMenu.bitmap)));
		model.setBitmap(bitmap);
		return model;
	}

	private ContentValues createContentByModel(MenuModel model) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(DbDefines.TableMenu.menuId, model.getId());
		contentValues.put(DbDefines.TableMenu.menuName, model.getName());
		contentValues.put(DbDefines.TableMenu.imageLink, model.getImageLink());
		contentValues.put(DbDefines.TableMenu.description, model.getDescription());
		byte[] img = ConvertImg.getBytes(model.getBitmap());
		contentValues.put(DbDefines.TableMenu.bitmap, img);
		return contentValues;
	}

	public int insertMenuModel(MenuModel model) {
		int value = -1;
		this.open();
		try {
			// Assign values to add...
			ContentValues contentValues = createContentByModel(model);

			// Add to database
			value = (int) this.mDb.insert(DbDefines.MenuModel, "",
					contentValues);
		} finally {
			this.close();
		}

		return value;
	}

	public int insertMenuModels(ArrayList<MenuModel> models) {
		if (models == null) {
			return 0;
		}
		
		int value = 0;
		int result = -1;
		this.open();
		try {
			// Assign values to add...
			ContentValues contentValues = null;
			for (MenuModel model : models) {
				contentValues = createContentByModel(model);
	
				// Add to database
				result = (int) this.mDb.insert(DbDefines.MenuModel, "",
						contentValues);
				Log.i(tag, "insertMenuModels(): result="+result+", model: "+model.toString());
				if (result != -1) {
					value++;
				}
			}
		} finally {
			this.close();
		}

		return value;
	}

	public int deleteMenuModel(int menuID) {
		int result = 0;
		this.open();
		try {
			result = this.mDb.delete(DbDefines.MenuModel,
					DbDefines.TableMenu.menuId + " = " + menuID, null);
		} finally {
			this.close();
		}
		return result;
	}

	public synchronized int updateMenuModel(MenuModel model) {
		int value = -1;
		this.open();
		try {
			ContentValues contentValues = createContentByModel(model);
			// Add to database
			value = (int) this.mDb.update(DbDefines.MenuModel, contentValues,
					DbDefines.TableMenu.menuId + " = " + model.getId(),
					null);
			if (value == 0) {
				this.close();
			}
		} finally {
			this.close();
		}
		return value;
	}

	public MenuModel loadMenuModel(int menuID) {
		this.open();
		// getting data from database
		Cursor cursor = null;
		MenuModel model = null;
		try {
			cursor = this.mDb.query(DbDefines.MenuModel, new String[] {
					DbDefines.TableMenu.menuId, DbDefines.TableMenu.menuName,
					DbDefines.TableMenu.imageLink, DbDefines.TableMenu.description,
					DbDefines.TableMenu.bitmap },
					DbDefines.TableMenu.menuId + " = " + "'" + menuID + "'",
					null, "", "", "", "1");

			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				model = createMenuModel(cursor);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return model;
	}

	public ArrayList<MenuModel> selectAll() {
		this.open();

		// getting data from database
		Cursor cursor = null;
		MenuModel model = null;
		ArrayList<MenuModel> models = new ArrayList<MenuModel>();
		try {
			cursor = mDb.rawQuery("SELECT * FROM " + DbDefines.MenuModel, null); // TODO refer

			while (cursor.moveToNext()) {
				model = createMenuModel(cursor);
				models.add(model);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return models;
	}
	
	/**
	 * Delete database
	 */
	public void deleteDatabase() {
		this.open();
		mDb.execSQL("DELETE FROM " + DbDefines.MenuModel);
		this.close();
	}
	
	/**
	 * Create database
	 */
	public void createDatabase() {
		this.open();
		DatabaseHelper.createMenuTable(mDb);
		this.close();
	}
}
